drop table if exists ustore_foreign_key;
create table ustore_foreign_key
(
    col int unique
) with (storage_type = 'ustore');
create table ustore_foreign_key_deferrable
(
    c1 int,
    c2 int,
    foreign key (c2) references ustore_foreign_key (col) deferrable
) with (storage_type = 'ustore');
insert into ustore_foreign_key values(1);

-- 约束生效
insert into ustore_foreign_key_deferrable values(1, 1);
insert into ustore_foreign_key_deferrable values(1, 2);

-- 去除列约束正常插入
alter table ustore_foreign_key_deferrable drop constraint if exists ustore_foreign_key_deferrable_c2_fkey;
insert into ustore_foreign_key_deferrable values(1, 2);

-- 有不符合条件数据时无法添加列约束
alter table ustore_foreign_key_deferrable add constraint ustore_foreign_key_deferrable_c2_key foreign key (c2) references ustore_foreign_key (col) deferrable;

-- 删除重复数据添加列约束成功
delete from ustore_foreign_key_deferrable where c1 = 1;
alter table ustore_foreign_key_deferrable add constraint ustore_foreign_key_deferrable_c2_key foreign key (c2) references ustore_foreign_key (col) deferrable;

begin;
set constraints all deferred;
insert into ustore_foreign_key_deferrable values(1, 2);
end;

begin;
set constraints all immediate;
insert into ustore_foreign_key_deferrable values(1, 2);
end;

begin;
set constraints all deferred;
insert into ustore_foreign_key_deferrable values(1, 2);
set constraints all immediate;
end;

drop table ustore_foreign_key;
drop table ustore_foreign_key_deferrable;